﻿using BLL;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统.新增窗体;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.财务管理
{
    public partial class 销售客诉报表 : Form
    {
        public 销售客诉报表()
        {
            InitializeComponent();
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.DateTime = dateEdit2.DateTime.AddDays(-QueryTime.间隔);
            CreateGrid.Create(this.Name, gridView1);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
              MainForm.mainform.AddMidForm (  new 销售客诉单 (new Model.CustomerComplaints (),Model.FormUseful.新增 ) );
            }
            catch(Exception ex){
                Tools.ShowMessageBox.ShowErrorBox(ex.Message );
            }
        }
        #region 弹出菜单
        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }
        #endregion

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = null, dt = gridControl1.DataSource as DataTable };
            fm.ShowDialog();
        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "销售客诉报表");
        }

        private void 生产客诉报表_Load(object sender, EventArgs e)
        {
            Query();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改销售客诉单") == true)
            {
                var doc = Connect.dbcontext.QueryOneResult<CustomerComplaints>(x => x.DocNum == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DocNum").ToString());
                if (doc.IsAudit)
                {
                    MainForm.mainform.AddMidForm(new 销售客诉单(doc, Model.FormUseful.查看));
                }
                else
                {
                    MainForm.mainform.AddMidForm(new 销售客诉单(doc, Model.FormUseful.修改));
                }
            } 
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            { 
                var docnum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DocNum").ToString();
                var doc = Connect.dbcontext.QueryOneResult<CustomerComplaints>(x => x.DocNum == docnum);
                if (doc.IsAudit)
                {
                    Tools.ShowMessageBox.ShowErrorBox("该单据已经审核！不能删除");
                    return;
                }
                else
                { 
                    Connect.dbcontext.BeginTransaction();
                    Connect.dbcontext.Delete<CustomerComplaints>(x => x.DocNum == docnum);
                    Connect.dbcontext.Delete<CustomerComplaintsDetail>(x => x.DocNum == docnum);
                    Connect.dbcontext.Commit();
                }
            }
            catch(Exception ex)
            {
                Connect.dbcontext.Rollback();
                Tools.ShowMessageBox.ShowErrorBox("删除的时候发生了错误:" + ex.Message);
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
            gridControl1.DataSource = Connect.dbcontext.Query($"select * from CustomerComplaints c left join danjumingxitable d on c.DocNum=d.danhao where c.SupplierName like '%{txtksmc.Text }%' and c.DocDate between '{dateEdit1.DateTime.Date}' and '{dateEdit2.DateTime.Date.AddDays (1)}' and c.DocStyle='{DanjuLeiXing.销售客诉单.DocName }'");
        }

        private void 审核通过ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var docnum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DocNum").ToString();
                var doc = Connect.dbcontext.QueryOneResult<CustomerComplaints>(x => x.DocNum == docnum);
                if (doc.IsAudit)
                {
                    Connect.dbcontext.Commit();
                    Tools.ShowMessageBox.ShowErrorBox("该单据已经审核！审核失败");
                    return;
                }
                Connect.dbcontext.BeginTransaction();
                doc.IsAudit = true;
                var danju = new DanjuTable() {  ksbh = doc.SupplierNum,
                    ksmc = doc.SupplierName,
                    own = User.user.own,
                    Qiankuan = "欠款",
                    Hanshui = "含税",
                    je = doc.ComplaintsMoney, totalmoney = 0,djlx=DanjuLeiXing.销售客诉单.DocName, dh = doc.DocNum };
                财务BLL.减少应收款 (danju);
                来往明细BLL.增加来往记录(danju);               
                订单BLL.增加费用(danjumingxitableService.Getdanjumingxitablelst (x=>x.danhao ==docnum ), danju);
                Connect.dbcontext.Update(doc);
                Connect.dbcontext.Commit();
                Tools.ShowMessageBox.ShowInfoBox("审核成功!");
                Query();
            }
            catch(Exception ex)
            {
                Connect.dbcontext.Rollback();
                Tools.ShowMessageBox.ShowErrorBox("审核的时候发生了错误！" + ex.Message);
            }
        }

        private void 单据反审ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {               
                var docnum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DocNum").ToString();
                var doc = Connect.dbcontext.QueryOneResult<CustomerComplaints>(x => x.DocNum == docnum);
                if (!doc.IsAudit)
                {
                    Tools.ShowMessageBox.ShowErrorBox("该单据还没审核！反审核失败");
                    return;
                }
                Connect.dbcontext.BeginTransaction();
                doc.IsAudit = false;
                
                var danju = new DanjuTable() { ksbh =doc.SupplierNum,ksmc=doc.SupplierName ,
                    own=User.user.own ,
                    Qiankuan="欠款",
                    Hanshui="含税",
                    je = doc.ComplaintsMoney, totalmoney = 0, djlx = DanjuLeiXing.销售客诉单.DocName, dh = doc.DocNum };
                财务BLL.增加应收款(danju);
                来往明细BLL.删除来往记录(danju);
                订单BLL.增加费用(danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == docnum), danju);
                Connect.dbcontext.Update(doc);
                Connect.dbcontext.Commit();
                Tools.ShowMessageBox.ShowInfoBox("反审核成功!");
                Query();
            }
            catch (Exception ex)
            {
                Connect.dbcontext.Rollback();
                Tools.ShowMessageBox.ShowErrorBox("审核的时候发生了错误！" + ex.Message);
            }
        }

        private void txtksmc_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 客户选择() { linkman = new LXR() { MC = "", ZJC = "" } };
            fm.ShowDialog();
           txtksmc .Text = fm.linkman.MC;
            Query();
        }
    }
}
